package com.mytaobao.main.firsttab.view;


import android.content.Context;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mytaobao.R;

import java.lang.ref.WeakReference;
import java.util.Timer;
import java.util.TimerTask;

public class ReverseClock extends LinearLayout{
    private TextView tvTaohaigouHour;
    private TextView tvTaohaigouMinute;
    private TextView tvTaohaigouSecond;

    public ReverseClock(Context context) {
        super(context);
        init(context);
    }

    public ReverseClock(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ReverseClock(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.reverse_clock, this, true);
        tvTaohaigouHour = (TextView)view.findViewById(R.id.haitaogou_hour);
        tvTaohaigouMinute = (TextView)view.findViewById(R.id.haitaogou_minute);
        tvTaohaigouSecond = (TextView)view.findViewById(R.id.haitaogou_second);
    }

    Runnable runnable = new MyRunnable(this);

    static class MyRunnable implements Runnable {
        private WeakReference<ReverseClock> viewWeakInstance;
        Handler SubHandler;
        MyRunnable(ReverseClock reverseClock) {
            viewWeakInstance = new WeakReference<ReverseClock>(reverseClock);

            HandlerThread handlerThread = new HandlerThread("ReverseClockCompute");
            handlerThread.start();
            SubHandler = new Handler(handlerThread.getLooper());
            SubHandler.postDelayed(this, 1000);
        }
        ReverseClock getReference(WeakReference<ReverseClock> viewWeakInstance){
            ReverseClock reverseClock = viewWeakInstance == null ? null : viewWeakInstance.get();
            return reverseClock;
        }
        ClockData clockData = new ClockData("01", "20", "28");
        @Override
        public void run() {
            clockData.nextSecond = computeNext(clockData.nextSecond);
            if (clockData.nextSecond == "59") {
                clockData.nextMinute = computeNext(clockData.nextMinute);;
                if(clockData.nextMinute == "59") {
                    clockData.nextHour = computeNext(clockData.nextHour);;
                }
            }
            ReverseClock reverseClock = getReference(viewWeakInstance);
            if(reverseClock != null && reverseClock.isAttachedToWindow() && reverseClock.isShown()) {
                Message message = Message.obtain(reverseClock.handler,1, clockData);
                reverseClock.handler.sendMessage(message);
            }
            SubHandler.postDelayed(this, 1000);
        }

        public String computeNext(String current) {
            int currentInt = Integer.parseInt(current);
            if(-- currentInt < 0) {
                return "59";
            }
            String next = (currentInt + "").length() > 1 && !(currentInt + "").contains("-")? (currentInt + "") : "0" + (currentInt + "");
            return next;
        }
    };

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            ClockData clockData = (ClockData) msg.obj;
            tvTaohaigouHour.setText(clockData.nextHour);
            tvTaohaigouMinute.setText(clockData.nextMinute);
            tvTaohaigouSecond.setText(clockData.nextSecond);
            super.handleMessage(msg);
        }
    };

    static class ClockData {
        public String nextHour;
        public String nextMinute;
        public String nextSecond;
        public ClockData(String nextHour, String nextMinute, String nextSecond) {
            this.nextHour = nextHour;
            this.nextMinute = nextMinute;
            this.nextSecond = nextSecond;
        }
    }

}
